/**
 * Application Name : MetaData
 * <p/>
 * Project Name : iDea
 * <p/>
 * Inputs : TODO <List Program input and configuration files>
 *
 * Parameters : TODO <List command line parameters for this program>
 *
 * Dependencies : TODO <List the libraries or other applications that this
 * program depends on>
 *
 * Outputs : TODO <List the files this program will output to>
 *
 * Description : TODO <A brief description of the program's function>
 *
 * Detailed Design : TODO <Include name and location of the detail design
 * document>
 *
 * Program History : Author : Massimo Renke <massimo.renke@census.gov>
 * Date : Apr 25, 2013 / 3:39:34 PM Changes : First created.
 * <p/>
 *
 * Other : TODO <Include any other information that someone who will maintain
 * this code should know>
 * <p/>
 */
package idea.sfo.viewer.utilites;


import idea.sfo.viewer.graphics.ColorSpec;
import idea.sfo.viewer.graphics.Drawable;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;


/**
 *
 * @author Massimo Renke <massimo.renke@census.gov>
 */
public class ImageWriter
{

    /**
     * Creates an image in JPEG format.
     * <p/>
     * @param fileName
     * @param imageSize
     * @param graphics
     * @throws IOException
     */
    public static void jpegImageWriter(
            String fileName ,
            Dimension imageSize ,
            ArrayList<Drawable> graphics )
            throws IOException
    {
        //
        BufferedImage bi = new BufferedImage(
                imageSize.width ,
                imageSize.height ,
                BufferedImage.TYPE_INT_RGB );

        //
        Graphics2D g2d = bi.createGraphics();

        // set the background color.
        g2d.setColor( ColorSpec.getDefaultBackgroundColor() );
        g2d.fillRect( 0 , 0 , imageSize.width , imageSize.height );

        for ( Drawable graphic : graphics )
        {
            graphic.draw( g2d );
        }

        ImageIO.write( bi , "jpg" , new File( fileName ) );
    }


    /**
     * Creates an image in PNG format.
     * <p/>
     * @param fileName
     * @param imageSize
     * @param graphics
     * @throws IOException
     */
    public static void pngImageWriter(
            String fileName ,
            Dimension imageSize ,
            ArrayList<Drawable> graphics )
            throws IOException
    {
        //
        BufferedImage bi = new BufferedImage(
                imageSize.width ,
                imageSize.height ,
                BufferedImage.TYPE_INT_RGB );

        //
        Graphics2D g2d = bi.createGraphics();

        // set the background color.
        g2d.setColor( ColorSpec.getDefaultBackgroundColor() );
        g2d.fillRect( 0 , 0 , imageSize.width , imageSize.height );

        for ( Drawable graphic : graphics )
        {
            graphic.draw( g2d );
        }

        ImageIO.write( bi , "bmp" , new File( fileName ) );
    }

}
